//Title: Emphasis Snippet
//Credit to: Spuzzum
//Contributed by: Spuzzum


//This handy proc emphasises a user's string if they surround their text with
// slashes (for italics), asterisks (for bolding), and underscores (for
// underlining).
//If you're using this in conjunction with BYOND's html_encode() proc, always
// use it *after* you call html_encode to ensure that the emphasis HTML doesn't
// get encoded as well.
//Text must be surrounded -- otherwise the emphasis symbol will appear all by
// itself.  That is, you must have an even number of emphasis symbols in your
// string -- otherwise the last symbol will appear literally.  This is by in-
// tentional design, so you can still use a single asterisk, underscore, or
// slash in a particular string (usually to tell people how to use emphasis).
//Note well that this will completely ruin a URL string -- this will not work
// well in any program where your users need to legitimately output URLs.  It
// would take substantial modification to prevent this from operating on URLs.

//Note: if you don't want users to be able to post URLs, then take a look at
// the strip_url proc in my Strip HTML Snippet.


//Add this definition to your DME file above the BEGIN INCLUDES section, and
// you'll be able to use the American spelling in your code.
#define emphasize(X) emphasise(X)


//Change any of these to specify the symbol, and comment out the line to remove
// that form of emphasis.
#define EMPHASISE_ITALICS   "`"
#define EMPHASISE_UNDERLINE "~"
#define EMPHASISE_BOLD      "#"


//You don't have to modify anything below this point.


proc/emphasise(string as text)
	var/pos = 0
	var/pos2 = 0
	var/minpos = 0

#ifdef EMPHASISE_ITALICS
	//Italicise
	pos = findtext(string,EMPHASISE_ITALICS)
	while(pos)
		pos2 = findtext(string,EMPHASISE_ITALICS,pos+1)
		minpos = pos2 + 4
		if(pos2)
			string = copytext(string,1,pos) + "<i>" + copytext(string,pos+1,pos2) + "</i>" + \
				copytext(string,pos2+1)
			pos = findtext(string,EMPHASISE_ITALICS,minpos)
		else
			break
#endif

#ifdef EMPHASISE_BOLD
	//Bold
	pos = findtext(string,EMPHASISE_BOLD)
	while(pos)
		pos2 = findtext(string,EMPHASISE_BOLD,pos+1)
		minpos = pos2 + 4
		if(pos2)
			string = copytext(string,1,pos) + "<b>" + copytext(string,pos+1,pos2) + "</b>" + \
				copytext(string,pos2+1)
			pos = findtext(string,EMPHASISE_BOLD,minpos)
		else
			break
#endif

#ifdef EMPHASISE_UNDERLINE
	//Underline
	pos = findtext(string,EMPHASISE_UNDERLINE)
	while(pos)
		pos2 = findtext(string,EMPHASISE_UNDERLINE,pos+1)
		minpos = pos2 + 4
		if(pos2)
			string = copytext(string,1,pos) + "<u>" + copytext(string,pos+1,pos2) + "</u>" + \
				copytext(string,pos2+1)
			pos = findtext(string,EMPHASISE_UNDERLINE)
		else
			minpos = 0
			break
#endif

	return string